<?php
class Product_Service_Package
{
	public function __construct(){
		
	}

	public static function add($row){
		$Obj = new Product_Model_Package();
		if(isset($row['customer_id']) && $row['customer_id'] ==='' ){
			$row['customer_id'] = '0';
		}
		$error = self::validate($row);		
		$lastInsertId = '';
		if(!$error){
			$lastInsertId = $Obj->add($row);	//返回被更新的行数
		}else{
			return array('ask'=>0,'pp_id'=>'','msg'=>$error);
		}
		if($lastInsertId){
			return array('ask'=>1,'pp_id'=>$lastInsertId,'msg'=>array('Add Success.'));
		}else {
			return array('ask'=>0,'pp_id'=>'','msg'=>array('Add Failed.'));
		}
	}
	
	public static function update($row,$id){
		$Obj = new Product_Model_Package();
		if(isset($row['customer_id']) && $row['customer_id'] ==='' ){
			$row['customer_id'] = 0;
		}
		$error1 = self::checkPackageUse($id,'package_id');
		$error2 = self::validate($row);	
		$error = array_merge($error1,$error2);
		$rows_affected = '';
		if(!$error){
			$rows_affected = $Obj->update($row,$id);	//返回被更新的行数
		}else{
			return array('ask'=>0,'rows'=>$rows_affected,'msg'=>$error);
		}				
		if($rows_affected){
			return array('ask'=>1,'rows'=>$rows_affected,'msg'=>array('Update Success.'));
		}else {
			return array('ask'=>0,'rows'=>'','msg'=>array('Update Failed.'));
		}
	}
	
	public static function delete($id){
		$Obj = new Product_Model_Package();
		$check = self::checkPackageUse($id,'package_id');
		$rows_affected = '';
		if(!$check){
			$rows_affected = $Obj->delete($id);
		}else{
			return array('ask'=>0,'rows'=>$rows_affected,'msg'=>'包材被使用，不能删除.');
		}
		if($rows_affected){
			return array('ask'=>1,'rows'=>$rows_affected,'msg'=>'Delete Success.');
		}else {
			return array('ask'=>0,'rows'=>'','msg'=>'Delete Failed.');
		}
	}
	 
	public static function getByPpid($pp_id){		
		$obj = new Product_Model_Package();
		return $obj->getByPpid($pp_id);		
	}
	
	public static function getByValue($value,$field){
		$Obj = new Product_Model_Package();
		return $Obj->getByValue($value,$field);	
	}
	
	public function getByCond($cond,$type,$order,$count,$offset){
		$Obj = new Product_Model_Package();	
		return $Obj->getByCond($cond,$type,$order,$count,$offset);		
	}

	public static function getAll($warehouseId,$customerId = null){
		$packageService = new Product_Model_Package();
		$packages = $packageService->getAll($warehouseId,$customerId);
		if($packages instanceof Zend_Db_Table_RowSet){
			return $packages->toArray();
		}
		return false;
	}
	
	/**
	 * 获取当前用户可用的包材
	 * @param $warehouseId 仓储ID
	 * @param $customerId  客户ID
	 * @return  返回一个二维数组，当前用户所有可用的包材
	 * 如果没有可用包材，返回空
	 */
	public static function getAllAvailable($warehouseId,$customerId){
		$Obj = new Product_Model_Package();
		$packageArr = array();
		$tmpArr1 = $Obj->getAll($warehouseId,'0')->toArray();
		$tmpArr2 = $Obj->getAll($warehouseId,$customerId)->toArray();
		
		$packageArr = array_merge($tmpArr1,$tmpArr2);		
		if(is_array($packageArr) && count($packageArr)){
			return $packageArr;
		}else{
			return array();
		}
	}
	
	/**
	 * 检查包材名称 是否被使用
	 * @param $value 包材名称
	 * @return  如果被使用返回true，反之返回false
	 */
	public static function checkPackageName($value){
		$Obj = new Product_Model_Package();
		$row = $Obj->getByValue($value,'pp_name');
		if($row){
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 检查包材使用被使用
	 * @param $value 对应字段的值
	 * @param $field 字段名称
	 * @return  如果被使用返回true，反之返回false
	 */
	public static function checkPackageUse($value,$field){
		$error = array();
		$Obj = new Product_Model_PackageMap();
		$row = $Obj->getByValue($value,$field);
		if($row){
			$error[] = '包材被使用，不能修改.';
		}
		return $error;
	}
	
	/**
	 * 检查包材状态 是否可用
	 * @param $value 对应字段的值
	 * @param $field 字段名称
	 * @return  如果可用 返回true，反之返回false
	 */
	public static function checkPackageStatus($value,$field){
		$row = self::getByValue($value,$field);
		if($row['status']){
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 验证表单提交的值
	 * @param $row 一维数组
	 * @return  验证通过 返回true，反之返回错误信息提示的数组
	 */
	public static function validate($row){
		$error = array();
		if(isset($row['pp_name'])){
			if($row['pp_name'] ===''){
				$error[] = '包材名称不能为空.';
			}else{
				$checkName = self::checkPackageName($row['pp_name']);
				if($checkName){
					$error[] = '包材名称已被使用.';
				}
			}
		}
		if(isset($row['customer_id']) && $row['customer_id'] !==0 && $row['customer_id'] !=='0' ){			
			$customerInfo = Customer_Service_Customer::getByCustomerId($row['customer_id']);
			if(!$customerInfo){
				$error[] = '客户ID 错误.';
			}
		}
		return $error;		
	}

}